// pages/teacher-appointment-record/index.js
const handler = require("../../utils/function.js");
const { $Message } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current_page: 1,
    total_page: 0,
    recordData:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = wx.getStorageSync('user_info').id;
    this.initData(id);
  },
  initData: function (id) {
    var para = {
      user_id: id,
      current_page: this.data.current_page
    };
    var that = this;
    
    handler.requestData("tearchAppointRecord", para, function (res) {
      if (res.success == true) {
        that.setData({
          spinShow: false,
          recordData: res.data.data,
          total_page: res.data.total
        })
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });

  },
  call_phone_handler:function(e){
  
    handler.callPhoneHandler(e.currentTarget.dataset.phone);
  },
  //接单
  accept:function(e){
    var toid = e.currentTarget.dataset.id;//
    var para ={tid:toid};
    var that = this;
    handler.requestData("acceptOrder", para, function (res) {
      if (res.success == true) {
        wx.showToast({
          title: "接单成功",
          icon: 'loading'
        })
        var id = wx.getStorageSync('user_info').id;
        that.initData(id);
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
  },
  //complete
  complete:function(e){
    var toid = e.currentTarget.dataset.id;//
    var para = { tid: toid };
    var that = this;
    handler.requestData("completeOrder", para, function (res) {
      if (res.success == true) {
        wx.showToast({
          title: "确认成功",
          icon: 'loading'
        })
        var id = wx.getStorageSync('user_info').id;
        that.initData(id);
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
  },
  //cancel
  cancel:function(e){
    var toid = e.currentTarget.dataset.id;//
    var that = this;
    handler.requestData("tearchCancelOrder", { oid: toid }, function (res) {
      if (res.success == true) {
        // $Message({
        //   content: res.message,
        //   type: 'success'
        // });
        wx.showToast({
          title: "取消订单成功",
          icon: 'loading'
        })
        that.onPullDownRefresh();
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var id = wx.getStorageSync('user_info').id;
    this.initData(id);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //上拉加载更多
    var current_page = this.data.current_page;
    var total_page = this.data.total_page;
    if (total_page < current_page * 10) {
      console.log("已经是全部数据");
    } else {
      this.setData({
        current_page: current_page + 1
      })
      wx.showNavigationBarLoading() //在标题栏中显示加载
      var id = wx.getStorageSync('user_info').id;
      // var user_type = wx.getStorageSync('user_info').user_type;
      this.initData(id);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})